﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.ReportViewer;
using MSBTC.Model;

namespace MSBTC.WinForms
{
    public partial class MaterialsInventoryForm : Form
    {
        MDIParent mdipar = new MDIParent();
        PMSContext dbcontext = new PMSContext();
        MaterialsInventory dbmi = new MaterialsInventory();
        private string Datamanip;
        private int rowselect;
        public MaterialsInventoryForm()
        {
            InitializeComponent();
        }

        private void MaterialsInventoryForm_Load(object sender, EventArgs e)
        {
            RefreshFields();
            RefreshDB();

            cmbMname.SelectedIndex = 0;
            //retrieve all data of customers
            var mat = (from c in dbcontext.Mat_Maintenances
                       select c).ToList();
            if (mat != null)
            {
                foreach (var c in mat)
                {
                    //add customer name in combo box
                    cmbMname.Items.Add(c.Material_Name.ToString());
                }
            }
            else
            {
                MessageBox.Show("There are no material listed, please add materials in Materials Maintenance in main menu", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }


            ///#############################################################################################################################
            ///

            var uni = (from c in dbcontext.Units
                       select c).ToList();
            if (uni != null)
            {
                foreach (var c in uni)
                {
                    //add customer name in combo box
                    cmbUnit.Items.Add(c.Units.ToString());
                }
            }
            else
            {
                MessageBox.Show("There are no Unit listed.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

        }

        #region Functions
        private void RefreshDB()
        {
            //retrieve all data
            var mtrlinv = (from m in dbcontext.MaterialsInventorys
                           select m).ToList();

            LVMaterialInv.Items.Clear();
            if (mtrlinv.Count != 0)
            {
                foreach (var sqlMtrl in mtrlinv)
                {
                    //add items in listview box
                    ListViewItem lvitems = LVMaterialInv.Items.Add(sqlMtrl.MaterialsInventoryid_PK.ToString());

                    lvitems.SubItems.Add(sqlMtrl.Material_Name.ToString());
                    //lvitems.SubItems.Add(sqlMtrl.Brand_Name.ToString());
                    lvitems.SubItems.Add(sqlMtrl.Qty.ToString());
                    lvitems.SubItems.Add(sqlMtrl.Unit.ToString());
                    lvitems.SubItems.Add(sqlMtrl.UnitCost.ToString());
                    lvitems.SubItems.Add(sqlMtrl.QtyUsed.ToString());
                    var a= sqlMtrl.Qty.ToString();
                    var b = sqlMtrl.QtyUsed.ToString();
                    var c = double.Parse(a) - double.Parse(b);
                    lvitems.SubItems.Add(c.ToString());
                    
                }
            }

        }

        private void ClearFields()
        {
            txtID.Clear();
            cmbMname.SelectedIndex = 0;
            txtMBName.Clear();
            txtMQuantity.Clear();
            cmbUnit.SelectedIndex = 0;
            txtSearch.Clear();
            txtUCost.Clear();
        }

        private void DisableFields()
        {
            txtID.Enabled = false;
            cmbMname.Enabled = false;
            txtMBName.Enabled = false;
            txtMQuantity.Enabled = false;
            cmbUnit.Enabled = false;
            txtUCost.Enabled = false;
        }

        private void EnableFields()
        {
            txtID.Enabled = false;
            cmbMname.Enabled = true;
            txtMBName.Enabled = true;
            txtMQuantity.Enabled = true;
            cmbUnit.Enabled = true;
            txtUCost.Enabled = true;
        }

        private void RefreshFields()
        {
            DisableFields();
            ClearFields();

            btnNew.Enabled = true;
            btnDel.Enabled = false;
            btnEdit.Enabled = false;
            btnSave.Enabled = false;
        }
        #endregion

        private void btnNew_Click(object sender, EventArgs e)
        {
            Datamanip = "Add";

            EnableFields();
            ClearFields();

            btnSave.Enabled = true;
            btnNew.Enabled = false;
            LVMaterialInv.Enabled = false;
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            DialogResult YN = MessageBox.Show("Do you want to Delete it?", "Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (YN == DialogResult.Yes)
            {
                var del = dbcontext.MaterialsInventorys.Find(Convert.ToInt32(txtID.Text));//get the same ID of purchase order to delete
                if (del != null)
                {
                    //delete the ID selected
                    dbcontext.Entry(del).State = EntityState.Deleted;
                    dbcontext.SaveChanges();
                }
                RefreshFields();
                RefreshDB();


            }
        }

        private void LVMaterialInv_SelectedIndexChanged(object sender, EventArgs e)
        {
            //focus the selected items
            rowselect = LVMaterialInv.FocusedItem.Index;


            //getting the information from the listview to textbox
            txtID.Text = LVMaterialInv.Items[rowselect].SubItems[0].Text;
            cmbMname.Text = LVMaterialInv.Items[rowselect].SubItems[1].Text;
            txtMBName.Text = LVMaterialInv.Items[rowselect].SubItems[2].Text;
            txtMQuantity.Text = LVMaterialInv.Items[rowselect].SubItems[3].Text;
            cmbUnit.Text = LVMaterialInv.Items[rowselect].SubItems[4].Text;
            txtUCost.Text = LVMaterialInv.Items[rowselect].SubItems[5].Text;

            txtSearch.Enabled = true;
            btnDel.Enabled = true;
            btnEdit.Enabled = true;
            btnSave.Enabled = false;
            btnNew.Enabled = true;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Datamanip = "Edit";

            EnableFields();

            btnSave.Enabled = true;
            btnNew.Enabled = false;
            btnDel.Enabled = false;
            btnEdit.Enabled = false;

        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshFields();
            LVMaterialInv.Enabled = true;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearFields();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            if ((cmbUnit.SelectedIndex == 0 || cmbUnit.SelectedIndex == 4 || cmbUnit.SelectedIndex == 18 || cmbUnit.SelectedIndex == 24))
            {
                MessageBox.Show("Please fill up the Unit", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            else
            {

                if (Datamanip == "Add")
                {
                    var add = (from m in dbcontext.MaterialsInventorys
                               where m.Material_Name == cmbMname.Text
                               select m).ToList();

                    if (add.Count == 0)
                    {
                        dbmi.Material_Name = cmbMname.Text;
                        dbmi.Brand_Name = txtMBName.Text;
                        dbmi.Qty = Convert.ToInt32(txtMQuantity.Text);
                        dbmi.Unit = cmbUnit.SelectedItem.ToString();
                        dbmi.UnitCost = Convert.ToInt32(txtUCost.Text);

                        var x = cmbUnit.Text;
                        dbmi.Unit = x;

                        dbcontext.Entry(dbmi).State = EntityState.Added;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();
                        RefreshDB();

                    }
                    else
                    {
                        foreach (var m in add)
                        {
                            if (m.Material_Name == cmbMname.Text && m.Brand_Name == txtMBName.Text)
                            {
                                DialogResult YN = MessageBox.Show("You've entered an existing project, do you want to replace it?", "Data Exist", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                                if (YN == DialogResult.Yes)
                                {
                                    //last
                                    var replace = dbcontext.MaterialsInventorys.Find(m.MaterialsInventoryid_PK);

                                    replace.Material_Name = cmbMname.Text;
                                    replace.Brand_Name = txtMBName.Text;
                                    replace.Qty = Convert.ToInt32(txtMQuantity.Text);
                                    replace.Unit = cmbUnit.SelectedItem.ToString();
                                    replace.UnitCost = Convert.ToInt32(txtUCost.Text);

                                    dbcontext.Entry(replace).State = EntityState.Modified;
                                    dbcontext.SaveChanges();

                                    MessageBox.Show("Successfully Modified.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    RefreshFields();
                                }
                            }
                            else
                            {

                                dbmi.Material_Name = cmbMname.Text;
                                dbmi.Brand_Name = txtMBName.Text;
                                dbmi.Qty = Convert.ToInt32(txtMQuantity.Text);
                                dbmi.Unit = cmbUnit.SelectedItem.ToString();
                                dbmi.UnitCost = Convert.ToInt32(txtUCost.Text);

                                dbcontext.Entry(dbmi).State = EntityState.Added;
                                dbcontext.SaveChanges();

                                MessageBox.Show("Successfully Saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                RefreshFields();
                                RefreshDB();
                            }
                        }
                    }
                    LVMaterialInv.Enabled = true;
                }

                else if (Datamanip == "Edit")
                {
                    DialogResult YN = MessageBox.Show("Do you want to apply changes?", "Modify", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (YN == DialogResult.Yes)
                    {
                        var replace = dbcontext.MaterialsInventorys.Find(Convert.ToInt32(txtID.Text));
                        replace.Material_Name = cmbMname.Text;
                        replace.Brand_Name = txtMBName.Text;
                        replace.Qty = Convert.ToInt32(txtMQuantity.Text);
                        replace.Unit = cmbUnit.Text;
                        replace.UnitCost = Convert.ToInt32(txtUCost.Text);

                        dbcontext.Entry(replace).State = EntityState.Modified;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Modified.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();
                        RefreshDB();
                    }
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtMMtrlName_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                RefreshDB();
            }
            else
            {
                //retrieve the same item name search by user
                var eq = (from p in dbcontext.MaterialsInventorys
                          where p.Material_Name.Contains(txtSearch.Text)
                          select p).ToList();

                LVMaterialInv.Items.Clear();
                if (eq.Count != 0)
                {

                    {
                        foreach (var sqlmat in eq)
                        {
                            ListViewItem lvitems = LVMaterialInv.Items.Add(sqlmat.MaterialsInventoryid_PK.ToString());
                            lvitems.SubItems.Add(sqlmat.Material_Name.ToString());
                            lvitems.SubItems.Add(sqlmat.Brand_Name.ToString());
                            lvitems.SubItems.Add(sqlmat.Qty.ToString());
                            lvitems.SubItems.Add(sqlmat.Unit.ToString());
                            lvitems.SubItems.Add(sqlmat.UnitCost.ToString());
                        }
                    }
                }

            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //MatrlRptForm MForm = new MatrlRptForm();

            //MForm.Show();
        }

        private void llAssign_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            AssignMtrlForm AForm = new AssignMtrlForm();

            AForm.Show();
        }

        private void cmbUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
           

        }

     }

 }

        
    